package com.jit.BluetoothKey.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Space;
import android.widget.Toast;

import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.activity.base.BaseActivity;
import com.jit.BluetoothKey.service.HelloteacherService;
import com.jit.BluetoothKey.utils.SharedPreferencesUtil;
import com.jit.BluetoothKey.utils.ToastUtils;
import com.jit.BluetoothKey.utils.UserFiled;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText account;
    private EditText password;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        /**
         *   判断当前是否是第一次登录 第一次登录直接跳转到主页面
         */
        if (SharedPreferencesUtil.getIsLogin(this,UserFiled.IS_LOGIN)&&SharedPreferencesUtil.getBoolean(this,UserFiled.BLE_AUTO_LOGIN)){
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }


        /**
         *  6.0 权限申请
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            }
        }

//        inProcess();
    }

    @Override
    protected void initView() {
        super.initView();
        account = (EditText) findViewById(R.id.login_account_edittext);
        password = (EditText) findViewById(R.id.login_password_edittext);
        login = (Button) findViewById(R.id.login_ensure_button);
    }

    @Override
    protected void initData() {
        super.initData();
        // TODO: 2018/3/2   代替用户输入 
    }

    @Override
    protected void initListner() {
        super.initListner();
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_ensure_button:
                Log.i("tag", "onClick: " + account.getText().toString());
                if (!account.getText().toString().equals("AZX") &&
                        !account.getText().toString().equals("BJEV")) {
                    ToastUtils.showToast(this,"请输入正确的用户名和密码");
                    return;
                }
                if (account.getText().toString().equals("AZX") && password.getText().toString().equals("AZX001")) {
                    // TODO: 2018/1/26   跳转逻辑和账号本地存储逻辑
                    userLoginSuccess(account.getText().toString(), password.getText().toString());
                    return;
                } else if (!password.getText().toString().equals("AZX001")&&!password.getText().toString().equals("BJEV001")) {
                    ToastUtils.showToast(this, "账号或密码错误，请验证后重试");
                }
                if (account.getText().toString().equals("BJEV") && password.getText().toString().equals("BJEV001")) {
                    // TODO: 2018/1/26   跳转逻辑和账号本地存储逻辑
                    userLoginSuccess(account.getText().toString(), password.getText().toString());
                    return;
                } else  {
                    ToastUtils.showToast(this, "账号或密码错误，请验证后重试");
                }
                break;
        }
    }

    /**
     * 用户登录存储用户名 密码 及跳转
     *
     * @param account
     * @param password
     */
    private void
    userLoginSuccess(String account, String password) {
        SharedPreferencesUtil.setString(this, UserFiled.ACCOUNT, account);
        SharedPreferencesUtil.setString(this, UserFiled.PASSWORD, password);
        if (SharedPreferencesUtil.getString(this,UserFiled.SK).equals("")){
            // TODO: 2018/2/7  注册码为空跳转设置注册码页面
            startActivity(new Intent(this,PingCodeInsertActivity.class));
            finish();
        }else {
            // TODO: 2018/2/7  注册码已存在跳转 主页面
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }
        SharedPreferencesUtil.setIsLogin(this,UserFiled.IS_LOGIN,true);
    }
}
